package com.igeek.yygh.controller;

import com.igeek.yygh.model.cmn.Dict;
import com.igeek.yygh.result.Result;
import com.igeek.yygh.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author zengxiangjun
 * @Date 2022/8/5  13:15 DictController
 */

// postman测试地址： http://localhost:8892/admin/hosp/dict
//Swagger测试地址：http://localhost:8892/swagger-ui.html

@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation("数据字典列表")
    @GetMapping("list/{id}")
    public Result findDictList(
            @ApiParam("数据字典的ID主键") @PathVariable("id") Long id
    ) {
        List<Dict> dicts = dictService.findAllById(id);
        return Result.ok(dicts);
    }

    //将Excel写至磁盘上
    @ApiOperation("导出数据字典")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) throws IOException {
        dictService.exportDictData(response);
    }

    //将磁盘上的Excel表格导入至内存，数据存储至磁盘
    @ApiOperation("导入数据字典")
    @PostMapping("importData")
    public void importData(MultipartFile file) throws IOException {
        dictService.importDictData(file);
    }

}